package com.songcha.module_mine.ui.fragment.message.my_comment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.songcha.library_base.mvvm.base.BaseMvvmFragment
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.util.ToastUtil
import com.songcha.module_mine.R
import com.songcha.module_mine.bean.MyBookCommentDataBean
import com.songcha.module_mine.databinding.MineFragmentMessageMycommentBinding
import com.songcha.module_mine.ui.adapter.MessageMyCommentAdapter
import com.songcha.module_mine.ui.adapter.MessageNotifyAdapter

class MessageMyCommentFragment:BaseMvvmFragment<MessageMyCommentViewModel,MineFragmentMessageMycommentBinding>(),OnItemChildClickListener {
    private var mAdapter: MessageMyCommentAdapter<MineFragmentMessageMycommentBinding>?=null

    override fun getLayoutId():Int{
        return R.layout.mine_fragment_message_mycomment
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        viewModel.getMyCommentList()

        viewModel.myCommentList.observe(this){data->
            initRcv(data)
        }

        viewModel.delMyCommentState.observe(this){data->
            if(data){
                ToastUtil.show("删除成功")
                viewModel.getMyCommentList()
            }else{

            }
        }

    }

    private fun initRcv(list:MutableList<MyBookCommentDataBean.MyBookCommentBean>){
        databinding.mineRcvMessageMycomment.apply {
            layoutManager=LinearLayoutManager(context)
            mAdapter=MessageMyCommentAdapter(list)
            mAdapter!!.setOnItemChildClickListener(this@MessageMyCommentFragment)
            adapter=mAdapter
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when(view.id){
            R.id.mine_iv_del_comment->{
                val db=CustomDialog.Companion.Builder(view.context)
                    db.setDialogMsg("是否删除该评论？")
                        .setOnConfirmListener {
                            viewModel.delMyComment(viewModel.myCommentList.value!![position].id)
                            true
                        }
                        .create()
                        .show()
            }
        }
    }
}